package com.coffee.excel.executor;

import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.coffee.excel.MochaExcelUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;


/**
 * 自定义样式拦截器-复杂表头样式的使用
 * @author: songkui
 * @since: 2023/2/10 23:23
 */
@Slf4j
public class HeadStyleWriteHandler extends HorizontalCellStyleStrategy {

    /**
     * key : 对应列  value: 背景 颜色
     */
    private Map<Integer, Short> colorMap;

    public HeadStyleWriteHandler(){
    }

    public HeadStyleWriteHandler(Map<Integer, Short> colorMap){
        this.colorMap = colorMap;
    }


    @Override
    protected void setHeadCellStyle(CellWriteHandlerContext context) {

        if (context.getRowIndex() == 1){
            WriteCellStyle writeCellStyle = null == colorMap || colorMap.isEmpty() ? MochaExcelUtil.buildHeadDefaultStyle() : MochaExcelUtil.buildHeadStyle(colorMap.get(context.getColumnIndex()));
            context.getFirstCellData().setWriteCellStyle(writeCellStyle);
        }else {
            context.getFirstCellData().setWriteCellStyle(MochaExcelUtil.buildHeadDefaultStyle());
        }


    }



}
